/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Session;

import javax.ejb.Stateless;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import Entity.Client;
import javax.persistence.Query;

/**
 *
 * @author guillaumediliberatore
 */
@Stateless
public class ClientSession implements ClientSessionLocal {

    @javax.persistence.PersistenceContext(unitName = "Assurance-ejbPU")
    private EntityManager em;
    
    
    @Override
    public boolean connexion(String email, String pwd){
        System.out.println("Test1");
        if(email != null && pwd != null) {
            System.out.println("Test2");
            //Client c = (Client) em.createNamedQuery("Client.findAll").getSingleResult();
            Query test = em.createNamedQuery("Client.findByEmail");
            System.out.println("Test3");
            test.setParameter("email",  email) ;
            System.out.println("Test4");
            Client c = (Client) test.getSingleResult() ;
            System.out.println("Test5");
            
            if(c.getEmail().equals(email) && c.getMotdepasse().equals(pwd)) {
               
                System.out.println("Test4");
                return true;
            }
            
        }
        return false;
        
    }

    
    @Override
    public boolean inscription(String adresse, String nom, String prenom, 
            String pwd, String datenaissance, String telephone, String sexe, String email){
        if(adresse != null && nom != null && prenom != null && pwd != null && datenaissance != null 
                && telephone != null && sexe != null && email != null ) {
            Client c = new Client(adresse, nom, prenom, pwd, datenaissance, telephone, sexe, email);
            em.persist(c);   
            return true;
        }
        return false;
    }
    
    
    @Override
    public boolean isNumberClientCorrect(String numeroClient){
        
        if (numeroClient != null && numeroClient!= ""){
            int numClientInt = Integer.parseInt(numeroClient);
            Integer numcontratInteger = (Integer) numClientInt;
            
            Query find = em.createNamedQuery("Client.findByIdclient");
            find.setParameter("idclient", numcontratInteger);
            
                            System.out.println("find.getResultList().size() " + find.getResultList().size());

            if(find.getResultList().size()!=0){
                System.out.println("true");
                 return true;
            }
            

            
        }
                        System.out.println("false");

        return false;
        
       
    }
    
    
    @Override
    public String getNumberClient(String login){
        Query q = em.createNamedQuery("Client.findByEmail");
        q.setParameter("email", login);
        Client c = (Client) q.getSingleResult();
        String result = c.getIdclient()+"";
        return result; 
        
    }
    
    
}
